<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="82px">
      <el-form-item label="短剧标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入短剧标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="导演" prop="director">
        <el-input v-model="queryParams.director" placeholder="请输入导演" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="主演列表" prop="actors">
        <el-input v-model="queryParams.actors" placeholder="请输入主演列表" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="短剧分类" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="请选择短剧分类" clearable>
          <el-option v-for="c in dramasCategories" :key="c.id" :label="c.name" :value="c.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="仅会员观看" prop="isVipOnly">
        <el-select v-model="queryParams.isVipOnly" placeholder="请选择仅会员观看" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="发布日期">
        <el-date-picker v-model="daterangeReleaseDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="短剧状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择短剧状态" clearable>
          <el-option v-for="dict in dict.type.dramas_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker v-model="daterangeUpdateTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['huoxingshi:dramas:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['huoxingshi:dramas:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['huoxingshi:dramas:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['huoxingshi:dramas:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dramasList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="短剧ID" align="center" prop="id" />
      <el-table-column label="短剧标题" align="center" prop="title" />
      <!-- <el-table-column label="详细简介" align="center" prop="description" /> -->
      <el-table-column label="短剧图片" align="center" prop="coverUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="导演" align="center" prop="director" />
      <el-table-column label="主演列表" align="center" prop="actors" />
      <el-table-column label="短剧分类" align="center" prop="categoryId">
        <template slot-scope="scope">
          {{ scope.row.category }}
        </template>
      </el-table-column>
      <el-table-column label="总集数" align="center" prop="totalEpisodes" />
      <el-table-column label="总时长" align="center" prop="duration" />
      <!-- <el-table-column label="解锁全剧价格" align="center" prop="price" width="120"/> -->
      <el-table-column label="发布日期" align="center" prop="releaseDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="短剧状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dramas_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="仅会员观看" align="center" prop="isVipOnly" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isVipOnly" />
        </template>
      </el-table-column>
      <el-table-column label="播放量" align="center" prop="viewCount" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleEpisodes(scope.row)"
            v-hasPermi="['huoxingshi:dramas:episodes']">选集</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['huoxingshi:dramas:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['huoxingshi:dramas:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改短剧信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="短剧标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入短剧标题" />
        </el-form-item>
        <el-form-item label="详细简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="短剧图片" prop="coverUrl">
          <image-upload v-model="form.coverUrl" />
        </el-form-item>
        <el-form-item label="导演" prop="director">
          <el-input v-model="form.director" placeholder="请输入导演" />
        </el-form-item>
        <el-form-item label="主演列表" prop="actors">
          <el-input v-model="form.actors" placeholder="请输入主演列表" />
        </el-form-item>
        <el-form-item label="短剧分类" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择短剧分类">
            <el-option v-for="c in dramasCategories" :key="c.id" :label="c.name" :value="c.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总集数" prop="totalEpisodes">
          <el-input v-model="form.totalEpisodes" placeholder="请输入总集数" />
        </el-form-item>
        <el-form-item label="总时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入总时长" />
        </el-form-item>
        <!-- <el-form-item label="全剧价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入解锁全剧价格" />
        </el-form-item> -->
        <el-form-item label="仅会员观看" prop="isVipOnly">
          <el-select v-model="form.isVipOnly" placeholder="请选择仅会员观看">
            <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布日期" prop="releaseDate">
          <el-date-picker clearable v-model="form.releaseDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择发布日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="短剧状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择短剧状态">
            <el-option v-for="dict in dict.type.dramas_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="episodesDialog.open" width="80%" append-body title="短剧选集">
      <Episodes v-if="episodesDialog.open" :dramasId="episodesDialog.dramaId" />
    </el-dialog>
  </div>
</template>

<script>
import { listDramas, getDramas, delDramas, addDramas, updateDramas } from "@/api/huoxingshi/dramas"
import { listCategories } from '@/api/huoxingshi/categories'

export default {
  name: "Dramas",
  dicts: ['sys_yes_no', 'sys_user_sex', 'dramas_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 短剧信息表格数据
      dramasList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeReleaseDate: [],
      // 备注时间范围
      daterangeCreateTime: [],
      // 备注时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        director: null,
        actors: null,
        categoryId: null,
        isVipOnly: null,
        releaseDate: null,
        status: null,
        createTime: null,
        updateTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "短剧标题不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "详细简介不能为空", trigger: "blur" }
        ],
        coverUrl: [
          { required: true, message: "短剧图片不能为空", trigger: "blur" }
        ],
        director: [
          { required: true, message: "导演不能为空", trigger: "blur" }
        ],
        actors: [
          { required: true, message: "主演列表不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "短剧分类不能为空", trigger: "change" }
        ],
        totalEpisodes: [
          { required: true, message: "总集数不能为空", trigger: "blur" }
        ],
        duration: [
          { required: true, message: "总时长不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "解锁全剧价格不能为空", trigger: "blur" }
        ],
        isVipOnly: [
          { required: true, message: "仅会员观看不能为空", trigger: "change" }
        ],
        releaseDate: [
          { required: true, message: "发布日期不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "短剧状态不能为空", trigger: "change" }
        ],
      },
      episodesDialog: {
        open: false,
        dramaId: null
      },
      dramasCategories: []
    }
  },
  components: {
    Episodes: () => import("@/views/huoxingshi/episodes/index")
  },
  created() {
    this.getList()
    listCategories({ pageNum: -1 }).then((res) => {
      this.dramasCategories = res.rows;
    })
  },
  methods: {
    handleEpisodes(row) {
      this.episodesDialog.dramaId = row.id;
      this.episodesDialog.open = true;
    },
    /** 查询短剧信息列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeReleaseDate && '' != this.daterangeReleaseDate) {
        this.queryParams.params["beginReleaseDate"] = this.daterangeReleaseDate[0]
        this.queryParams.params["endReleaseDate"] = this.daterangeReleaseDate[1]
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0]
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1]
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0]
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1]
      }
      listDramas(this.queryParams).then(response => {
        this.dramasList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        coverUrl: null,
        director: null,
        actors: null,
        categoryId: null,
        totalEpisodes: null,
        duration: null,
        price: null,
        isVipOnly: null,
        releaseDate: null,
        viewCount: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeReleaseDate = []
      this.daterangeCreateTime = []
      this.daterangeUpdateTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加短剧信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDramas(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改短剧信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDramas(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addDramas(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除短剧信息编号为"' + ids + '"的数据项？').then(function () {
        return delDramas(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('huoxingshi/dramas/export', {
        ...this.queryParams
      }, `dramas_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
